# Maintainer: Simon Zeni <simon@bl4ckb0ne.ca>
pkgname=sdl3
pkgver=3.1.6
pkgrel=0
pkgdesc="development library designed to provide low level access to audio, keyboard, mouse, joystick and graphics"
url="https://www.libsdl.org"
arch="all"
license="Zlib"
depends_dev="$pkgname mesa-dev"
makedepends="
	alsa-lib-dev
	cmake
	eudev-dev
	jack-dev
	libsamplerate-dev
	libx11-dev
	libxcursor-dev
	libxext-dev
	libxi-dev
	libxkbcommon-dev
	libxrandr-dev
	libxrender-dev
	libxscrnsaver-dev
	libusb-dev
	pipewire-dev
	pulseaudio-dev
	samurai
	wayland-dev
	wayland-protocols
	$depends_dev
	"
subpackages="$pkgname-dbg $pkgname-doc $pkgname-dev"
source="https://github.com/libsdl-org/SDL/releases/download/preview-$pkgver/SDL3-$pkgver.tar.gz"
builddir="$srcdir/SDL3-$pkgver"

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	CFLAGS="$CFLAGS -flto=auto -ffat-lto-objects" \
	cmake -B build -G Ninja \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DCMAKE_JOB_POOLS=thr=3 \
		-DCMAKE_JOB_POOL_LINK=thr \
		-DBUILD_SHARED_LIBS=True \
		-DSDL_TESTS="$(want_check && echo ON || echo OFF)" \
		-DSDL_ALSA=ON \
		-DSDL_JACK=ON \
		-DSDL_CLOCK_GETTIME=ON \
		-DSDL_RPATH=OFF \
		-DSDL_X11=ON \
		-DSDL_WAYLAND=ON \
		-DSDL_STATIC=ON \
		-DSDL_SHARED=ON \
		$CMAKE_CROSSOPTS
	ninja -C build
}

check() {
	case "$CARCH" in
		# Test is broken https://github.com/libsdl-org/SDL/issues/9219
		ppc64le) ctest --test-dir build -E testautomation;;
		*) ctest --test-dir build;;
	esac
}

package() {
	DESTDIR="$pkgdir" ninja -C build install
}

sha512sums="
0a48c1697749d937103cd0a483f62fd82c4a62fe0072f8e62a3964c2abbadbc7892aea2159e9a927621db08297569d446538a82892dfebfd15ae619d4837f06f  SDL3-3.1.6.tar.gz
"
